package com.kestrel.space.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.kestrel.space.common.utils.Generator;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.core.mapper.OrgMemberMapper;
import com.kestrel.space.core.model.OrgMember;
import com.kestrel.space.core.query.OrgMemberQuery;
import com.kestrel.space.core.service.OrgMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author yebolin
 * @date 2024/9/5
 */
@Service
public class OrgMemberServiceImpl implements OrgMemberService {
    @Autowired
    private OrgMemberMapper orgMemberMapper;

    @Override
    public List<OrgMember> listContent(OrgMemberQuery query, LargePage page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        OrgMember orgMember = BeanUtil.copyProperties(query, OrgMember.class);
        return orgMemberMapper.select(orgMember);
    }

    @Override
    public String saveOrUpdate(OrgMember orgMember) {
        String message = "插入成功";
        if (orgMember.getId() == null) {
            orgMember.setId(Generator.generateId());
            orgMemberMapper.insert(orgMember);
        } else {
            message = "更新成功";
            orgMemberMapper.updateByPrimaryKeySelective(orgMember);
        }
        return message;
    }

    @Override
    public String batchSaveOrUpdate(List<OrgMember> orgMemberList) {
        String message = "批量插入成功";
        if (CollectionUtils.isEmpty(orgMemberList)) {
            return "集合为空";
        }
        if(orgMemberList.get(0).getId() == null){
            for (OrgMember orgMember : orgMemberList) {
                orgMember.setId(Generator.generateId());
                orgMemberMapper.insert(orgMember);
            }
        } else {
            message = "批量更新成功";
            for (OrgMember orgMember : orgMemberList) {
                orgMember.setId(Generator.generateId());
                orgMemberMapper.updateByPrimaryKey(orgMember);
            }
        }
        return message;
    }
}
